package net.qhzw.vip.qiyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.config.FileTemplate;
import net.qhzw.vip.qiyu.entity.DtsBarManage;
import net.qhzw.vip.qiyu.entity.DtsBarManage;
import net.qhzw.vip.qiyu.entity.DtsBarManageSecondary;
import net.qhzw.vip.qiyu.handler.ApiException;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.DtsBarManageSecondaryService;
import net.qhzw.vip.qiyu.service.DtsBarManageService;
import net.qhzw.vip.qiyu.service.DtsBarManageService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("dts/barManage")
@Api(tags = "电柜维护管理")
public class DtsBarManageController {


    @Autowired
    private DtsBarManageService dtsBarManageService;

    @Autowired
    private DtsBarManageSecondaryService dtsBarManageSecondaryService;
    @Autowired
    private FileTemplate fileTemplate;




    @ApiOperation(value = "分页查询维修维护")
    @GetMapping
    public JsonResult<Page<DtsBarManage>> findByPage(Page<DtsBarManage> page,DtsBarManage dtsBarManage) throws ParseException {
        //Long shopId = SecurityUtils.getLoginUser().getShopId();
        Page<DtsBarManage> dtsBarManagePage = dtsBarManageService.findByPage(page,dtsBarManage);
        return JsonResult.ok(dtsBarManagePage);
    }

    @ApiOperation(value = "根据ID查询电柜维护")
    @GetMapping("{id}")
    public JsonResult<DtsBarManage> findDtsBarManage(@PathVariable Long id) {
        DtsBarManage dtsBarManagePage = dtsBarManageService.getById(id);
        return JsonResult.ok(dtsBarManagePage);
    }


    @ApiOperation(value = "添加电柜信息")
    @PostMapping
    public JsonResult addBar(@RequestBody DtsBarManage dtsBarManage) {

        boolean save = dtsBarManageService.addBar(dtsBarManage);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "修改电柜维护")
    @PutMapping
    public JsonResult updateDtsBarManage(@RequestBody DtsBarManage dtsBarManage) {
        boolean save = dtsBarManageService.updateDtsBarManage(dtsBarManage);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "设置电柜维修状态")
    @PutMapping("setStatus")
    public JsonResult setStatus(@RequestBody DtsBarManage dtsBarManage) {
        boolean save = dtsBarManageService.updateDtsBarManage(dtsBarManage);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "删除维修维护")
    @DeleteMapping("{id}")
    public JsonResult<DtsBarManage> findDtsBarManage(@PathVariable String id) {
        List<String> list = Arrays.asList(id.split(","));
        boolean remove = dtsBarManageService.removeByIds(list);
        if (remove) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }
    @ApiOperation(value = "表格导入电柜信息值")
    @PostMapping("importBar")
    public JsonResult importRecharge(@RequestParam("file") MultipartFile file) {
        //Long shopId = SecurityUtils.getLoginUser().getShopId();
        //if (shopId == 1) {
        //    throw new ApiException("您不能充值，请登录商户账号进行操作！");
        //}
         dtsBarManageService.importRecharge(file);
        return JsonResult.ok();
    }

    //@ApiOperation(value = "上传文件")
    //@PostMapping("upload")
    //public JsonResult upload(@RequestBody DtsBarManage dtsBarManage) {
    //
    //    boolean update = false;
    //
    //    if (dtsBarManage.getId() != null && dtsBarManage.getPicture() != null && !dtsBarManage.getPicture().isEmpty()) {
    //        dtsBarManage.setStatus((byte)1);
    //        update = dtsBarManageService.updateById(dtsBarManage);
    //
    //        DtsBarManageSecondary dtsBarManageSecondary = new DtsBarManageSecondary();
    //        dtsBarManageSecondary.setId(IdWorker.getId());
    //        dtsBarManageSecondary.setElectricCabinetCode(dtsBarManage.getElectricCabinetCode());
    //        dtsBarManageSecondary.setElectricCabinetName(dtsBarManage.getElectricCabinetName());
    //        dtsBarManageSecondary.setCreated(new Date());
    //        if (dtsBarManage.getFaultIdenti() == 0 && dtsBarManage.getFaultIdenti() != null) {
    //            dtsBarManageSecondary.setProhibitedNum(dtsBarManage.getProhibitedWarehouseNum());
    //            dtsBarManageSecondary.setStatus((byte) 0);
    //        }else {
    //            dtsBarManageSecondary.setFaultNum(dtsBarManage.getFaultCupboarddoorNum());
    //            dtsBarManageSecondary.setStatus((byte) 1);
    //        }
    //
    //        dtsBarManageSecondary.setPosition(String.join(",",dtsBarManage.getPosition()));
    //        dtsBarManageSecondary.setCreated(new Date());
    //        dtsBarManageSecondary.setPicture(dtsBarManage.getPicture());
    //         dtsBarManageSecondary.setRepairNum(dtsBarManage.getPosition().size());
    //        dtsBarManageSecondaryService.save(dtsBarManageSecondary);
    //
    //    }
    //    if (dtsBarManage.getId() != null && dtsBarManage.getRemark() != null){
    //        DtsBarManageSecondary one = dtsBarManageSecondaryService.getOne(new LambdaQueryWrapper<DtsBarManageSecondary>()
    //                .eq(DtsBarManageSecondary::getDtsBarId, dtsBarManage.getId())
    //                .eq(DtsBarManageSecondary::getStatus,0));
    //        //update = dtsBarManageSecondaryService.updateById(dtsBarManage);
    //
    //    }
    //    if (update) {
    //        return JsonResult.ok();
    //    }
    //    return JsonResult.fail();
    //}

}
